package com.benshou.bcss.common.db.mybatisplus;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.benshou.bcss.common.db.BaseEntity;
import com.benshou.bcss.common.tenant.TenantContext;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.schema.Column;

import java.util.List;

/**
 * 租户行级处理器
 *
 * @author <a href="mailto:liuzhenbang@benshou.com">Liu Zhenbang</a>
 */
public class TenantLineHandlerImpl implements TenantLineHandler {

    @Override
    public Expression getTenantId() {
        // TODO exception 选取
        return new LongValue(BaseEntity.getCurrentTenantId());
    }

    @Override
    public String getTenantIdColumn() {
        return BaseEntity.COLUMN_TENANT_ID;
    }

    @Override
    public boolean ignoreTable(String tableName) {
        return TenantContext.isPublic();
    }

    @Override
    public boolean ignoreInsert(List<Column> columns, String tenantIdColumn) {
        return TenantContext.isPublic() || TenantLineHandler.super.ignoreInsert(columns, tenantIdColumn);
    }
}
